package com.jrx.anytxn.reporting.job.unionFile.step;

import com.jrx.anytxn.reporting.config.UnionFileConfig;
import com.jrx.anytxn.reporting.constant.ReportingConstant;
import com.jrx.anytxn.reporting.job.unionFile.bean.TlPaymentBean;
import com.jrx.anytxn.reporting.service.IUnionFileService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UnionRepayFileWriter implements ItemWriter<TlPaymentBean> {

    @Autowired
    private IUnionFileService unionFileService;
    @Autowired
    private UnionFileConfig unionFileConfig;

    @Override
    public void write(List<? extends TlPaymentBean> items) throws Exception {
        if(CollectionUtils.isNotEmpty(items)){
            List<String> meaageList = new ArrayList<>();
            for(TlPaymentBean bean : items){
                meaageList.addAll(bean.getMessageList());
            }
            unionFileService.generateUnionFile(meaageList, unionFileConfig.getUnionFilePath(),
                    unionFileConfig.getUnionFileName(), ReportingConstant.UNION_FILE_TYPE_REPAY_SERIAL);
        }
    }
}
